# -*- coding: utf-8 -*-
"""
Created on Thu Nov 16 17:27:46 2017

@author: Administrator
"""
import tkinter
import xlrd
import win32com.client
import base64
from shifu import img
import os 
from tkinter import messagebox
import win32api
import win32clipboard as w
import win32con


class selfhelp():
    def __init__(self):
        
        
        self.root = tkinter.Tk()
        self.root.title = "自助复制查找区"
        tmp = open("tmp.ico","wb+")
        tmp.write(base64.b64decode(img))
        tmp.close()
        self.root.iconbitmap("tmp.ico")
        os.remove("tmp.ico")
        self.voice= win32com.client.Dispatch("SAPI.SpVoice")
        
        self.data=xlrd.open_workbook('形势与政策题库.xls')
        table1=self.data.sheets()[0]
        col1= table1.col_values(0)
        col17= table1.col_values(7)
        
        table2=self.data.sheets()[1]
        col2= table2.col_values(0)
        col27= table2.col_values(7)
        
        table3=self.data.sheets()[2]
        col3=table3.col_values(0)
        col32= table3.col_values(2)
        self.box1=[]
        self.box17=[]
        for i1 in col1:
            self.box1.append(i1.replace(' ',''))
        for i17 in col17:
            self.box17.append(i17)
            
        self.box2=[]
        self.box27=[]
        for i2 in col2:
            self.box2.append(i2.replace(' ',''))
        for i27 in col27:
            self.box27.append(i27)
            
        self.box3=[]
        self.box32=[]
        for i3 in col3:
            self.box3.append(i3.replace(' ',''))
        for i32 in col32:
            self.box32.append(i32)
        tkinter.Label(self.root,text='',width=2).grid(row=0, column=2)
        tkinter.Label(self.root,text='自助复制查找区').grid(row=0, column=1)

        tkinter.Label(self.root,text='要查找的题').grid(row=1, sticky = 'w')
        self.input8=tkinter.StringVar()#捕获用户输入
        self.box8=tkinter.Entry(self.root,textvariable=self.input8,width=40)#用户文本输入
        self.input8.set('')#输入框预设值
        self.box8.grid(row=1, column=1)

        tkinter.Label(self.root,text='对应的答案').grid(row=2, sticky = 'w')
        self.input9=tkinter.StringVar()#捕获用户输入
        self.box9=tkinter.Entry(self.root,textvariable=self.input9,width=40)#用户文本输入
        self.input9.set('')#输入框预设值
        self.box9.grid(row=2, column=1)

        
        menubar = tkinter.Menu(self.root,bg="red")
        self.root.config(menu = menubar)
        operationMenu1 = tkinter.Menu(menubar, tearoff = 0) 
        menubar.add_cascade(label = "操作", menu = operationMenu1)
        operationMenu1.add_command(label = "打开试题库", command = self.opensitiku)
        operationMenu1.add_separator()
        operationMenu1.add_command(label = "退出", command = self.root.destroy)
        operationMenu1.add_separator()
        
        self.box_question=[]
        self.box_answer=[]
        for item in self.box1:
            self.box_question.append(item)
        for item in self.box2:
            self.box_question.append(item)
        for item in self.box3:
            self.box_question.append(item)
        for item in self.box17:
            self.box_answer.append(item)
        for item in self.box27:
            self.box_answer.append(item)
        for item in self.box32:
            self.box_answer.append(item)
        
        tkinter.Label(self.root,text='').grid(row=4, sticky = 'w')
        
        tkinter.Button(self.root,text=("查找"),command=self.find,width=10,height=1,bg='green').grid(row=5,column=1)
        
        tkinter.Button(self.root,text=("清空"),command=self.xx,height=1,bg='red').grid(row=1,column=3)
        self.root.mainloop()
    def getText(self):
        w.OpenClipboard()
        d = w.GetClipboardData(win32con.CF_TEXT)
        w.CloseClipboard()
        return d
    def xx(self):
        self.input8.set('')
        self.input9.set('')
    def find(self):
        if self.input8.get():
            an=str(self.input8.get())
            answer=an.replace(' ','')                      
            n=-1
            for a in self.box_question:
                n+=1
                if answer.replace('\n','') in a:
                    self.input9.set(self.box_answer[n])
                    #self.voice.Speak('选'+self.box_answer[n])
                    break
            if n>len(self.box_question)-2:
                self.input9.set('题库中无此题目，请自行百度')
                self.voice.Speak('题库中无此题目，请自行百度')
    def opensitiku(self):
        if os.path.exists("形势与政策题库.xls"):
            win32api.ShellExecute(0,'open', "形势与政策题库.xls", '','',1)
        else:
            messagebox.showinfo('提示','文件不存在')
def run():
    selfhelp()
run()
